package com.globagames.strategicgames.view.battles.actions
{
	import com.globagames.isometric.core.model.SimpleObject;
	import com.globagames.isometric.core.view.IsoMap;
	import com.globagames.isometric.core.view.actions.IAction;
	import com.globagames.strategicgames.model.Soldier;
	import com.globagames.strategicgames.model.StrategicGameModelLocator;
	
	import flash.events.Event;
	import flash.events.EventPhase;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.UIComponent;

	[Event(name="selectedSimpleObject")]		
	public class SelectAction extends UIComponent implements IAction {
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		public var _soldier:Soldier;
		public var simpleObject:SimpleObject;
		
		public var point3d:Point;
				
		override protected function createChildren():void{
			x = 0;
			y = 0;
			super.createChildren();			
			this.width = model.coreModel.screenWidth;
			this.height = model.coreModel.screenHeight;	
			invalidateProperties();
		}
		
		public function mouseDownHandler(event:MouseEvent):void {	
			if(event.eventPhase == EventPhase.BUBBLING_PHASE ) {							
				if( model.coreModel.isoMap.mouseOverObject){	
					this.systemManager.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
					this.systemManager.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
				} 			
			}
		} 
		public function mouseUpHandler(event:MouseEvent):void {		
			this.systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			this.systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
				//trace("SelectAction::mouseUpHandler");						
				if( model.coreModel.isoMap.mouseOverObject){	
					//trace("SelectAction::mouseUpHandler 2");
					simpleObject = model.coreModel.isoMap.currentIsoObjectMouseOver as SimpleObject;
					point3d = model.coreModel.isoMap.currentIsoObjectPoint3d;
					var e:Event = new Event("selectedSimpleObject");
					dispatchEvent(e);
					//trace("SelectAction::mouseUpHandler 3");
				} 			
			
		} 
		public function mouseMoveHandler(event:MouseEvent):void {								
			this.systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			this.systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		} 
        
        
		public function attach():void {							
			this.systemManager.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler,false);
		}
		
		public function detach():void {		
			this.systemManager.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler,false);
			this.systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			this.systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		public function get soldier ():Soldier {
			return _soldier;
		}
		public function set soldier (s:Soldier):void {
			_soldier = s;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {			
			super.updateDisplayList(unscaledWidth, unscaledHeight);				
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			invalidateDisplayList();			
		}
		

	}
}